package pe.edu.upc.dew.Grupo1.web;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.context.support.WebApplicationContextUtils;

import pe.edu.upc.dew.Grupo1.model.Local;
import pe.edu.upc.dew.Grupo1.model.Pelicula;
import pe.edu.upc.dew.Grupo1.service.LocalService;
import pe.edu.upc.dew.Grupo1.service.PeliculaService;

public class ListaCarteleraServlet extends HttpServlet  {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private LocalService localService;   
	private PeliculaService peliculaService; 
	
	@Override
	public void init(ServletConfig config) throws ServletException {
		this.localService = WebApplicationContextUtils
		.getWebApplicationContext(config.getServletContext()).getBean(
				"localService", LocalService.class);
		
		this.peliculaService = WebApplicationContextUtils
		.getWebApplicationContext(config.getServletContext()).getBean(
				"peliculaService", PeliculaService.class);
	
	}
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		
		Local local=new Local();
		local.setNombre("");
		List<Local> listaLocales=localService.GetLocalesPorRazonSocial(local);
		
		req.setAttribute("locales", listaLocales);
				
		req.getRequestDispatcher("cartele.jsp").forward(req,resp);
	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
			
		Local local=new Local();
		local.setNombre("");
		List<Local> listaLocales=localService.GetLocalesPorRazonSocial(local);
		
		req.setAttribute("locales", listaLocales);
		
		String paramCodigoLocal=req.getParameter("codigoLocal");
		
		if(paramCodigoLocal!= null && !paramCodigoLocal.equals(""))
		{		
			int codigoLocal=Integer.parseInt(paramCodigoLocal);
			
			List<Pelicula> listaPeliculas=peliculaService.getPeliculasParaCarteleraLocal(codigoLocal);
						
			req.setAttribute("peliculas", listaPeliculas);
			req.setAttribute("codigoLocal", paramCodigoLocal);
		}
		
		req.getRequestDispatcher("cartele.jsp").forward(req,resp);
	}
}
